package com.nibian.nibian.activity;


import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.nibian.nibian.R;
import com.nibian.nibian.app.Umeng;
import com.nibian.nibian.bluetooth.BleProfileService;
import com.nibian.nibian.bluetooth.DebugLogger;
import com.nibian.nibian.bluetooth.ScannerFragment;
import com.nibian.nibian.bluetooth.UARTInterface;
import com.nibian.nibian.bluetooth.UARTService;
import com.nibian.nibian.fragment.SheBeiFragment;
import com.nibian.nibian.fragment.SheZhiFragment;
import com.nibian.nibian.fragment.ShuJuFragment;
import com.nibian.nibian.fragment.ThirdStyleFragment;
import com.nibian.nibian.utils.CRCUtils;
import com.nibian.nibian.utils.SharedPreferenceTools;
import com.nibian.nibian.utils.UiUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import butterknife.BindView;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import no.nordicsemi.android.log.ILogSession;
import no.nordicsemi.android.log.LocalLogSession;
import no.nordicsemi.android.log.Logger;

/**
 * @创建者 luck
 * @创建时间 2016/8/17 16:29
 * @描述 ${start - activity}
 * @更新者 $Author$
 * @更新时间 $Date$
 * @更新描述 ${TODO}
 */
public class MainActivity extends BaseActivity implements ScannerFragment.OnDeviceSelectedListener {

    @BindView(R.id.fl_content)
    FrameLayout mFl_content;                        //Fragment
    @BindView(R.id.btn_radiogroup)
    RadioGroup mBtn_radiogroup;                    //RadionGroup
    @BindView(R.id.rb_jiankong)
    RadioButton mRb_jiankong;                       //实时监控
    @BindView(R.id.rb_shuju)
    RadioButton mRrb_shuju;                       //历史数据
    @BindView(R.id.rb_shezhi)
    RadioButton mBb_shezhi;                       //参数设置
    @BindView(R.id.rb_shebei)
    RadioButton mRb_shebei;                       //设备信息
    @BindView(R.id.big_rl)
    RelativeLayout big_rl;                          //底部radioButton

    private boolean isFristSend = false;
    private boolean isCanshu = false;
    private boolean isRequstPermission = false;
    public static final int TRY_COUT = 5;
    private List<String> mList = new ArrayList();
    private int mTryCount;
    public static MainActivity sMainActivity;
    private String mLanguages_key;
    private int dataCount;

    public MainActivity() {
    }

    public static MainActivity getInstance() {
        return sMainActivity;
    }

    Fragment[] mFragments = {new ThirdStyleFragment(), new ShuJuFragment(), new SheZhiFragment(), new SheBeiFragment()};      //管理fragment

    /**
     * init - ui
     */
    @Override
    public int layoutId() {

        return R.layout.activity_main;
    }

    /**
     * init
     */
    @Override
    public void initView() {
        mLanguages_key = SharedPreferenceTools.getString(UiUtils.getContext(), "LANGUAGES_KEY", "zh");

        sendShiShiData();                           //初始化时发送第一个报文

        hideJianKongFra();
        MobclickAgent.onEvent(this, Umeng.START, System.currentTimeMillis() / 1000 + "");

        changeFragment(0);

        mBtn_radiogroup.check(R.id.rb_jiankong);     //在这里进行默认的第一个的选择
        mRb_jiankong.setTextColor(getResources().getColor(R.color.color_text_selected));
    }

    /**
     * 显示rediobutton
     */
    public void showJianKongFra() {
        big_rl.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏rediobutton
     */
    public void hideJianKongFra() {
        big_rl.setVisibility(View.GONE);
    }

    /**
     * Fragment进行切换 -   这里是运用replace实现
     *
     * @param
     */
    public void changeFragment(int i) {
        if (mFragments[i] == null) {
            throw new IllegalArgumentException("fragment不能为---null");
        }
        //第二参数就是可以区分tag
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fl_content, mFragments[i], "MyTag")                   //第二参数就是可以区分tag
//                .addToBackStack(null)
//                    .hide(mAlmaFragment[i])                                       //replace
                .commit();
    }

    /**
     * 底部监听
     */
    @Override
    public void initEvent() {
        mBtn_radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                mTryCount = 0;
                switch (checkedId) {
                    case R.id.rb_jiankong:
                        isFristSend = false;
                        //实实监控
                        SharedPreferenceTools.saveInt(UiUtils.getContext(), "isUi", 11);
                        big_rl.setVisibility(View.GONE);
                        changeFragment(0);
                        /**
                         * 实实监控 - 发送数据 -  这里注意的是默认是发送逆变的报文
                         */
                        sendShiShiData();
                        break;
                    case R.id.rb_shuju:                                                             //历史数据
                        SharedPreferenceTools.saveInt(UiUtils.getContext(), "isUi", 22);
                        changeFragment(1);
                        setTextColor();
                        setColorChoose(checkedId);
                        sendShuJuData();
                        break;
                    case R.id.rb_shezhi:                                                        //参数设置
                        changeFragment(2);
                        setTextColor();
                        setColorChoose(checkedId);
                        SharedPreferenceTools.saveInt(UiUtils.getContext(), "isUi", 33);
                        sendSheZhiData();
                        break;
                    case R.id.rb_shebei:                                                            //设备信息
                        SharedPreferenceTools.saveInt(UiUtils.getContext(), "isUi", 44);
                        changeFragment(3);
                        setTextColor();
                        setColorChoose(checkedId);
                        sendSheBeiData();
                        break;
                }
            }
        });
    }

    public void sendSheBeiData() {
        //在发送历史数据的时候 - 需要提前发送获取串口使用权
        if (isConnect) {
            mList.clear();
            isFristSend = false;
            isRequstPermission = false;
            mTryCount = 0;
            if (mUARTInterface != null) {
                final byte[] user = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x00, 0x0C, 0x54, 0x52, 0x41, 0x4E, 0x53, 0x50, 0x4F, 0x52, 0x54, 0x3d, 0x32, 0x0D, (byte) 0xAA, (byte) 0xAA};
                mUARTInterface.send(user);
            }
        }
    }

    public void sendSheZhiData() {
        if (isConnect) {
            mList.clear();
            isFristSend = false;
            isRequstPermission = false;
            mTryCount = 0;
            if (mUARTInterface != null) {
                final byte[] user = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x00, 0x0C, 0x54, 0x52, 0x41, 0x4E, 0x53, 0x50, 0x4F, 0x52, 0x54, 0x3d, 0x32, 0x0D, (byte) 0xAA, (byte) 0xAA};
                mUARTInterface.send(user);
            }
        }
    }

    public void sendShuJuData() {
        //在发送历史数据的时候 - 需要提前发送获取串口使用权
        if (isConnect) {
            mList.clear();
            isFristSend = false;
            isRequstPermission = false;
            mTryCount = 0;
            if (mUARTInterface != null) {
                final byte[] user = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x00, 0x0C, 0x54, 0x52, 0x41, 0x4E, 0x53, 0x50, 0x4F, 0x52, 0x54, 0x3d, 0x32, 0x0D, (byte) 0xAA, (byte) 0xAA};
                mUARTInterface.send(user);
            }
        }
    }

    /**
     * 设置字体颜色
     */
    public void setTextColor() {
        mRb_jiankong.setTextColor(getResources().getColor(R.color.bottom_tab_font_color));
        mRrb_shuju.setTextColor(getResources().getColor(R.color.bottom_tab_font_color));
        mBb_shezhi.setTextColor(getResources().getColor(R.color.bottom_tab_font_color));
        mRb_shebei.setTextColor(getResources().getColor(R.color.bottom_tab_font_color));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sMainActivity = this;

        EventBus.getDefault().register(this);
        ensureBLESupported();
        if (!isBLEEnabled()) {
            showBLEDialog();
            initView();
        }
        LocalBroadcastManager.getInstance(this).registerReceiver(mCommonBroadcastReceiver, makeIntentFilter());
    }

    /**
     * 根据点击使用字体颜色的选择
     *
     * @param checkedId
     */
    public void setColorChoose(int checkedId) {
        switch (checkedId) {
            case R.id.rb_jiankong:
                mRb_jiankong.setTextColor(getResources().getColor(R.color.color_text_selected));
                break;
            case R.id.rb_shuju:
                mRrb_shuju.setTextColor(getResources().getColor(R.color.color_text_selected));
                break;
            case R.id.rb_shezhi:
                mBb_shezhi.setTextColor(getResources().getColor(R.color.color_text_selected));
                break;
            case R.id.rb_shebei:
                mRb_shebei.setTextColor(getResources().getColor(R.color.color_text_selected));
                break;
            case 0:
                mRb_jiankong.setTextColor(getResources().getColor(R.color.color_text_selected));
                mRb_jiankong.setChecked(true);
                break;
            case 1:
                mRrb_shuju.setTextColor(getResources().getColor(R.color.color_text_selected));
                mRrb_shuju.setChecked(true);
                break;
            case 2:
                mBb_shezhi.setTextColor(getResources().getColor(R.color.color_text_selected));
                mBb_shezhi.setChecked(true);
                break;
            case 3:
                mRb_shebei.setTextColor(getResources().getColor(R.color.color_text_selected));
                mRb_shebei.setChecked(true);
                break;
        }
    }


    public static boolean isConnect = false;
    private UARTService.UARTBinder mService;
    private static final int REQUEST_ENABLE_BT = 2;
    private String mDeviceName;
    private static final String TAG = "MainActivity";
    private ILogSession mLogSession;
    private UARTService.UARTBinder mServiceBinder;
    private UARTInterface mUARTInterface;

    private final BroadcastReceiver mCommonBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(final Context context, final Intent intent) {
            final String action = intent.getAction();

            switch (action) {
                case UARTService.BROADCAST_UART_RX: {
                    final String extra_data = intent.getStringExtra(UARTService.EXTRA_DATA);
                    MobclickAgent.onEvent(MainActivity.this, Umeng.ALL_RECEIVE, extra_data);
                    showExtraDate(extra_data);
                    break;
                }
                case BleProfileService.BROADCAST_CONNECTION_STATE: {
                    final int state = intent.getIntExtra(BleProfileService.EXTRA_CONNECTION_STATE, BleProfileService.STATE_DISCONNECTED);

                    switch (state) {
                        case BleProfileService.STATE_CONNECTED: {
                            Log.e("tag", "连接成功" + "MainActivitymCommonBroadcastReceiver");
                            mDeviceName = intent.getStringExtra(BleProfileService.EXTRA_DEVICE_NAME);
                            onDeviceConnected();
                            break;
                        }
                        case BleProfileService.STATE_DISCONNECTED: {                    //断开连接将设备状态进行改变
                            Log.e("tag", "断开连接" + "MainActivitymCommonBroadcastReceiver");
                            onDeviceDisconnected();
                            mDeviceName = null;
                            break;
                        }
                        case BleProfileService.STATE_LINK_LOSS: {
                            //                            onLinklossOccur();
                            break;
                        }
                        case BleProfileService.STATE_CONNECTING:
                        case BleProfileService.STATE_DISCONNECTING:
                            // current implementation does nothing in this states
                        default:
                            // there should be no other actions
                            break;
                    }
                    break;
                }
                case BleProfileService.BROADCAST_SERVICES_DISCOVERED: {
                    //final boolean primaryService = intent.getBooleanExtra(BleProfileService.EXTRA_SERVICE_PRIMARY, false);
                    //final boolean secondaryService = intent.getBooleanExtra(BleProfileService.EXTRA_SERVICE_SECONDARY, false);
                    //if (primaryService) {
                    //onServicesDiscovered(secondaryService);
                    //} else {
                    //onDeviceNotSupported();
                    //}
                    Log.e(TAG, "BleProfileService.BROADCAST_SERVICES_DISCOVERED");
                    break;
                }
                case BleProfileService.BROADCAST_DEVICE_READY: {
                    //                    onDeviceReady();
                    Log.e(TAG, "BleProfileService.BROADCAST_DEVICE_READY");
                    break;
                }
                case BleProfileService.BROADCAST_BOND_STATE: {
                    final int state = intent.getIntExtra(BleProfileService.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
                    switch (state) {
                        case BluetoothDevice.BOND_BONDING:
                            //                            onBondingRequired();
                            Log.e(TAG, "BleProfileService.BOND_BONDING");
                            break;
                        case BluetoothDevice.BOND_BONDED:
                            Log.e(TAG, "BleProfileService.BOND_BONDED");
                            //                            onBonded();
                            break;
                    }
                    break;
                }
                case BleProfileService.BROADCAST_BATTERY_LEVEL: {
                    //final int value = intent.getIntExtra(BleProfileService.EXTRA_BATTERY_LEVEL, -1);
                    //if (value > 0)
                    //onBatteryValueReceived(value);
                    Log.e(TAG, "BleProfileService.BROADCAST_BATTERY_LEVEL");
                    break;
                }
                case BleProfileService.BROADCAST_ERROR: {
                    final String message = intent.getStringExtra(BleProfileService.EXTRA_ERROR_MESSAGE);
                    final int errorCode = intent.getIntExtra(BleProfileService.EXTRA_ERROR_CODE, 0);
                    onError(message, errorCode);
                    break;
                }
            }
        }
    };

    /**
     * 设备断开连接
     */
    public void onDeviceDisconnected() {
        isConnect = false;
        try {
            Logger.d(mLogSession, "Unbinding from the service...");
            unbindService(mServiceConnection);
            mService = null;

            Logger.d(mLogSession, "Activity unbinded from the service");
            onServiceUnbinded();
            mDeviceName = null;
            mLogSession = null;
        } catch (final IllegalArgumentException e) {
            // do nothing. This should never happen but does...
        }
    }

    public void onError(final String message, final int errorCode) {
        DebugLogger.e(TAG, "Error occurred: " + message + ",  error code: " + errorCode);
        showToast(message + "(" + errorCode + ")");
    }

    protected void showToast(final String message) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
            }
        });
    }

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @SuppressWarnings("unchecked")
        @Override
        public void onServiceConnected(final ComponentName name, final IBinder service) {
            final UARTService.UARTBinder bleService = mService = (UARTService.UARTBinder) service;
            mLogSession = mService.getLogSession();
            mUARTInterface = bleService;
            Logger.d(mLogSession, "Activity binded to the service");
            onServiceBinded(bleService);
            // update UI
            mDeviceName = bleService.getDeviceName();
            if (bleService.isConnected())
                onDeviceConnected();
        }

        @Override
        public void onServiceDisconnected(final ComponentName name) {
            Log.e("tag", "断开连接" + "MainActivitymonServiceDisconnected");
            Logger.d(mLogSession, "Activity disconnected from the service");
            mUARTInterface = null;
            mService = null;
            mDeviceName = null;
            mLogSession = null;
            onServiceUnbinded();
        }
    };

    protected void onServiceBinded(final UARTService.UARTBinder binder) {
        mServiceBinder = binder;
    }

    protected void onServiceUnbinded() {
        mServiceBinder = null;
    }

    protected int getDefaultDeviceName() {
        return R.string.uart_default_name;
    }

    /**
     * 连接成功
     */
    public void onDeviceConnected() {
        isConnect = true;
        EventBus.getDefault().post("connectSuccess");          //连接成功， 将连接状态进行改变
    }

    private static IntentFilter makeIntentFilter() {
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BleProfileService.BROADCAST_CONNECTION_STATE);
        intentFilter.addAction(BleProfileService.BROADCAST_SERVICES_DISCOVERED);
        intentFilter.addAction(BleProfileService.BROADCAST_DEVICE_READY);
        intentFilter.addAction(BleProfileService.BROADCAST_BOND_STATE);
        intentFilter.addAction(BleProfileService.BROADCAST_BATTERY_LEVEL);
        intentFilter.addAction(BleProfileService.BROADCAST_ERROR);
        intentFilter.addAction(UARTService.BROADCAST_UART_RX);
        return intentFilter;
    }

    private void ensureBLESupported() {
        if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
            Toast.makeText(this, R.string.no_ble, Toast.LENGTH_LONG).show();
            finish();
        }
    }

    protected boolean isBLEEnabled() {
        final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        final BluetoothAdapter adapter = bluetoothManager.getAdapter();
        return adapter != null && adapter.isEnabled();
    }

    protected void showBLEDialog() {
        final Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
    }


    public void onConnectClicked() {
        if (isBLEEnabled()) {
            if (mService == null) {
                showDeviceScanningDialog(null);
            } else {
                mService.disconnect();
            }
        } else {
            showBLEDialog();
        }
    }

    /**
     * text2发送的按钮点击事件
     *
     * @param view
     */
    public void onSendMessage(final View view) {
    }


    private void showDeviceScanningDialog(final UUID filter) {
        final ScannerFragment dialog = ScannerFragment.getInstance(filter);
        dialog.show(getSupportFragmentManager(), "scan_fragment");
    }

    @Override
    public void onDeviceSelected(BluetoothDevice device, String name) {

        final int titleId = getLoggerProfileTitle();
        if (titleId > 0) {
            mLogSession = Logger.newSession(getApplicationContext(), getString(titleId), device.getAddress(), name);
            // If nRF Logger is not installed we may want to use local logger
            if (mLogSession == null && getLocalAuthorityLogger() != null) {
                mLogSession = LocalLogSession.newSession(getApplicationContext(), getLocalAuthorityLogger(), device.getAddress(), name);
            }
        }
        mDeviceName = name;

        isConnect = false;
        // The device may not be in the range but the service will try to connect to it if it reach it
        Logger.d(mLogSession, "Creating service...");
        final Intent service = new Intent(this, getServiceClass());
        service.putExtra(BleProfileService.EXTRA_DEVICE_ADDRESS, device.getAddress());
        if (mLogSession != null)
            service.putExtra(BleProfileService.EXTRA_LOG_URI, mLogSession.getSessionUri());
        startService(service);
        Logger.d(mLogSession, "Binding to the service...");
        bindService(service, mServiceConnection, 0);
    }

    protected int getLoggerProfileTitle() {
        return 0;
    }

    protected Uri getLocalAuthorityLogger() {
        return null;
    }

    protected Class<? extends BleProfileService> getServiceClass() {
        return UARTService.class;
    }

    @Override
    public void onDialogCanceled() {
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            if (mService != null)
                mService.setActivityIsFinishing(isFinishing());

            Logger.d(mLogSession, "Unbinding from the service...");
            unbindService(mServiceConnection);
            mService = null;
            mUARTInterface = null;
            Logger.d(mLogSession, "Activity unbinded from the service");
            onServiceUnbinded();
            mDeviceName = null;
            mLogSession = null;
        } catch (final IllegalArgumentException e) {
            // do nothing, we were not connected to the sensor
        }
        EventBus.getDefault().unregister(this);
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mCommonBroadcastReceiver);
    }

    /**
     * 事件总线
     *
     * @param action
     */
    @Subscribe
    public void onEvent(String action) {
        switch (action) {
            case "startSearch":
                onConnectClicked();
                break;
            case "sendGuangFuKongZhi":
                MobclickAgent.onEvent(this, "guangfu");
                sendGuangFuData();
                break;
            case "sendChuNengDianYuan":
                MobclickAgent.onEvent(this, "chuneng");
                sendChuNengData();
                break;
            case "sendNiBianXiTong":
                MobclickAgent.onEvent(this, "nibian");
                sendShiShiData();
                break;
        }
    }

    public void sendGuangFuData() {
        if (isConnect) {
            mTryCount = 0;
            mList.clear();
            isFristSend = false;
            isRequstPermission = false;
            SharedPreferenceTools.saveInt(UiUtils.getContext(), "isUi", 55);
            if (mUARTInterface != null) {
                final byte[] user = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x00, 0x0C, 0x54, 0x52, 0x41, 0x4E, 0x53, 0x50, 0x4F, 0x52, 0x54, 0x3d, 0x32, 0x0D, (byte) 0xAA, (byte) 0xAA};
                mUARTInterface.send(user);
            }
        }
    }

    public void sendChuNengData() {
        if (isConnect) {
            mTryCount = 0;
            mList.clear();
            isFristSend = false;
            isRequstPermission = false;
            SharedPreferenceTools.saveInt(UiUtils.getContext(), "isUi", 66);
            final byte[] user = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x00, 0x0C, 0x54, 0x52, 0x41, 0x4E, 0x53, 0x50, 0x4F, 0x52, 0x54, 0x3d, 0x32, 0x0D, (byte) 0xAA, (byte) 0xAA};
            if (mUARTInterface != null) {
                mUARTInterface.send(user);
            }
        }
    }

    /**
     * 串口返回的数据 - 有可能数据是断断续续的过来， 所以这里直接保存 - 将数据传递之后 -  在进行解析
     *
     * @param extra_data
     */
    private void showExtraDate(String extra_data) {
        int isUi = SharedPreferenceTools.getInt(UiUtils.getContext(), "isUi", 0);           //从sp中获取对应的fragment的对应id， 然后将数据传递到每一个界面即可
        switch (isUi) {
            case 11:                //实实监控
//                ThirdStyleFragment.getInstance().setUiData(extra_data);
                Log.d("11", extra_data);
                break;
            case 22:                //历史数据
                //在发送使用权的时候 - 这边会进行数据的返回 - 如果是获得使用的权限 - 那么则将在发送报文
                //返回数据 - 这里我不知道返回的是什么数据
                Log.d("22", extra_data);
                if (isFristSend == false) {
                    if (extra_data.equals("ffff000c5452414e53504f52543d320daaaa")) {    //--     刚开始发送使用权 -  返回数据之后 -  在发送报文 - 在获取报文返回的数据
                        dataCount = 0;
                        mTryCount = 0;
                        /**
                         * 历史数据 - 发送数据
                         */
                        Log.d("extra_lishi", "进来了");
                        sendLishiData2(dataCount);
                    }
                } else {
                    MobclickAgent.onEvent(this, Umeng.HISTORY, "dataCount ==" + dataCount + "  extra_data=" + extra_data);
                    if (CRCUtils.judgeCrc(extra_data)) {
                        ShuJuFragment.getInstance().setUiData2(extra_data, dataCount);
                        dataCount++;
                        mTryCount = 0;
                        if (dataCount == 7) {
                            endDataTransform();
                        }
                    } else if (mTryCount < TRY_COUT) {
                        MobclickAgent.onEvent(this, Umeng.TRY, Umeng.HISTORY);
                        MobclickAgent.onEvent(this, Umeng.TRYDATA, Umeng.HISTORY + "   dataCount " + dataCount + extra_data);
                        mTryCount++;
                    } else {
                        MobclickAgent.onEvent(this, Umeng.TRYERROR, Umeng.HISTORY);
                        MobclickAgent.onEvent(this, Umeng.TRYDATA, Umeng.HISTORY + "   dataCount " + dataCount + extra_data);
                        Log.e("extra_lishi", "获取数据失败");
                        dataCount++;
                        mTryCount = 0;
                        if (dataCount == 7) {
                            endDataTransform();
                        }
                    }
                    sendLishiData2(dataCount);
                }
                break;
            case 33:                                                                                //参数设置
                Log.d("extra_canshu", extra_data);
                if (isFristSend == false) {
                    if (extra_data.equals("ffff000c5452414e53504f52543d320daaaa")) {
                        /**
                         * 参数设置 - 发送数据
                         */
                        dataCount = 0;
                        mTryCount = 0;
                        Log.d("33", "进来了");
                        sendCanShuData2(dataCount);
                    }
                } /*else if (isCanshu) {
                    MobclickAgent.onEvent(this, Umeng.SETTING_DIANYA, extra_data);
                    if (extra_data.contains("e003")) {
                        sendCanShuData2(dataCount);
                        isCanshu = false;
                    } else {
                        if (mTryCount < TRY_COUT) {
                            sendCanShuDianYa();
                            mTryCount++;
                        } else {
                            mTryCount = 0;
                            isCanshu = false;
                            sendCanShuData2(dataCount);
                        }
                    }
                }*/ else {
                    MobclickAgent.onEvent(this, Umeng.SETTING, "dataCount ==" + dataCount + "  extra_data=" + extra_data);
                    if (CRCUtils.judgeCrc(extra_data)) {
                        SheZhiFragment.getInstance().setUiData2(extra_data, dataCount);
                        dataCount++;
                        mTryCount = 0;
                        if (dataCount == 11) {
                            endDataTransform();
                        }
                    } else if (mTryCount < TRY_COUT) {
                        MobclickAgent.onEvent(this, Umeng.TRY, Umeng.SETTING);
                        MobclickAgent.onEvent(this, Umeng.TRYDATA, Umeng.SETTING + "   dataCount " + dataCount + extra_data);
                        mTryCount++;
                    } else {
                        MobclickAgent.onEvent(this, Umeng.TRYERROR, Umeng.SETTING);
                        MobclickAgent.onEvent(this, Umeng.TRYDATA, Umeng.SETTING + "   dataCount " + dataCount + extra_data);
                        dataCount++;
                        mTryCount = 0;
                        if (dataCount == 11) {
                            endDataTransform();
                        }
                        Log.e("extra_canshu", "获取数据失败");
                    }
                    sendCanShuData2(dataCount);
                }
                break;
            case 44:                                                                                //设备信息
                Log.d("44", extra_data);
                if (isFristSend == false) {
                    if (extra_data.equals("ffff000c5452414e53504f52543d320daaaa")) {
                        /**
                         * 设备信息 - 发送信息
                         */
                        dataCount = 0;
                        mTryCount = 0;
                        Log.d("44", "进来了");
                        sendSheBeiData2(dataCount);
                    }
                } else {
                    MobclickAgent.onEvent(this, Umeng.INFO, "dataCount ==" + dataCount + "  extra_data=" + extra_data);
                    if (CRCUtils.judgeCrc(extra_data)) {
                        SheBeiFragment.getInstance().setUiData(dataCount, extra_data);
                        dataCount++;
                        mTryCount = 0;
                        if (dataCount == 3) {
                            endDataTransform();
                        }
                    } else if (mTryCount < TRY_COUT) {
                        MobclickAgent.onEvent(this, Umeng.TRY, Umeng.INFO);
                        MobclickAgent.onEvent(this, Umeng.TRYDATA, Umeng.INFO + "   dataCount " + dataCount + extra_data);
                        mTryCount++;
                    } else {
                        MobclickAgent.onEvent(this, Umeng.TRYERROR, Umeng.INFO);
                        MobclickAgent.onEvent(this, Umeng.TRYDATA, Umeng.INFO + "   dataCount " + dataCount + extra_data);
                        dataCount++;
                        mTryCount = 0;
                        if (dataCount == 3) {
                            endDataTransform();
                        }
                    }
                    sendSheBeiData2(dataCount);
                }
                break;
            case 55:                                                                                //光伏控制
                if (isFristSend == false) {
                    if (extra_data.equals("ffff000c5452414e53504f52543d320daaaa")) {
                        /**
                         * 设备信息 - 发送信息
                         */
                        dataCount = 0;
                        mTryCount = 0;
                        sendGuangFuKongZhi(dataCount);
                    }
                } else {
                    MobclickAgent.onEvent(this, Umeng.GUANG_FU, "dataCount ==" + dataCount + "  extra_data=" + extra_data);
                    if (CRCUtils.judgeCrc(extra_data)) {
                        ThirdStyleFragment.getInstance().setGuangFuData(extra_data);
                        dataCount++;
                        mTryCount = 0;
                        if (dataCount == 1)
                            endDataTransform();
                    } else if (mTryCount < TRY_COUT) {
                        MobclickAgent.onEvent(this, Umeng.TRY, Umeng.GUANG_FU);
                        MobclickAgent.onEvent(this, Umeng.TRYDATA, Umeng.GUANG_FU + "   dataCount " + dataCount + extra_data);
                        mTryCount++;
                    } else {
                        MobclickAgent.onEvent(this, Umeng.TRYERROR, Umeng.GUANG_FU);
                        MobclickAgent.onEvent(this, Umeng.TRYDATA, Umeng.GUANG_FU + "   dataCount " + dataCount + extra_data);
                        mTryCount = 0;
                        dataCount++;
                        UiUtils.toast("获取光伏控制数据失败");
                        if (dataCount == 1)
                            endDataTransform();
                    }
                    sendGuangFuKongZhi(dataCount);
                }
                break;
            case 66:
                //储能电源
                Log.d("66", extra_data);
                if (isFristSend == false) {
                    if (extra_data.equals("ffff000c5452414e53504f52543d320daaaa")) {
                        /**
                         * 储能电源
                         */
                        dataCount = 0;
                        mTryCount = 0;
                        sendChuNengDianYuan(dataCount);
                    }
                } else {
                    MobclickAgent.onEvent(this, Umeng.CHU_NENG, "dataCount ==" + dataCount + "  extra_data=" + extra_data);
                    if (CRCUtils.judgeCrc(extra_data)) {
                        ThirdStyleFragment.getInstance().setChuNengData(dataCount, extra_data);
                        dataCount++;
                        mTryCount = 0;
                        if (dataCount == 3)
                            endDataTransform();
                    } else if (mTryCount < TRY_COUT) {
                        MobclickAgent.onEvent(this, Umeng.TRY, Umeng.CHU_NENG);
                        MobclickAgent.onEvent(this, Umeng.TRYDATA, Umeng.CHU_NENG + "   dataCount " + dataCount + extra_data);
                        mTryCount++;
                    } else {
                        MobclickAgent.onEvent(this, Umeng.TRYERROR, Umeng.CHU_NENG);
                        MobclickAgent.onEvent(this, Umeng.TRYDATA, Umeng.CHU_NENG + "   dataCount " + dataCount + extra_data);

                        mTryCount = 0;
                        dataCount++;
                        if (dataCount == 3)
                            endDataTransform();
                    }
                    sendChuNengDianYuan(dataCount);
                }
                break;
            case 77:                                                                              //逆变器系统
                Log.d("77", extra_data);
                mList.add(extra_data);
                MobclickAgent.onEvent(this, Umeng.NI_BIAN, "dataCount ==" + mList.size() + "  extra_data=" + extra_data);
                if (isFristSend == false) {
                    if (extra_data.equals("ffff000c5452414e53504f52543d310daaaa")) {
                        /**
                         * 主页发送 - 逆变器状态
                         */
                        mTryCount = 0;
                        sendZhuYeNiBianQiState();
                    }
                } else {
                    if (mList != null && mList.size() == 5) {
                        String s1 = mList.get(1);
                        String s2 = mList.get(2);
                        String s3 = mList.get(3);
                        String s4 = mList.get(4);
                        String s = GetValue(s1 + s2 + s3 + s4);
                        MobclickAgent.onEvent(this, Umeng.NI_BIAN, "final data ==" + s);
                        if (s != null && s.length() == 112) {
                            ThirdStyleFragment.getInstance().setNiBianStateData(mList);
                            Log.d("mList", mList.size() + "");
                            Log.d("mList", mList.toString());
                            endDataTransform();
                        } else if (mTryCount < TRY_COUT) {
                            MobclickAgent.onEvent(this, Umeng.TRY, Umeng.NI_BIAN);
                            MobclickAgent.onEvent(this, Umeng.TRYDATA, Umeng.NI_BIAN + "   final data=" + s);
                            mTryCount++;
                            sendZhuYeNiBianQiState();
                        } else {
                            MobclickAgent.onEvent(this, Umeng.TRYERROR, Umeng.NI_BIAN);
                            MobclickAgent.onEvent(this, Umeng.TRYDATA, Umeng.NI_BIAN + "   final data=" + s);
                            mTryCount = 0;
                            UiUtils.toast("获取逆变数据失败");
                            endDataTransform();
                        }
                        mList.clear();
                    }
                }
                break;
            case 88:                                                                                //回复出厂设置
                if (isFristSend != false) {
                    if (extra_data.equals("ffff000c5452414e53504f52543d320daaaa")) {
                        /**
                         * 发送回复出厂设置
                         */
                        if (mUARTInterface != null) {
                            final byte[] buffer = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x78, 0x00, 0x00, 0x00, 0x01, 0x60, 0x00, (byte) 0xAA, (byte) 0xAA};
                            mUARTInterface.send(buffer);
                            if (mLanguages_key.equals("zh")) {
                                UiUtils.toast("操作成功");
                            } else {
                                UiUtils.toast("Setting Successful");
                            }
                        }
                    }
                }
                break;
            case 99:
                if (isFristSend != false) {
                    MobclickAgent.onEvent(this, Umeng.CLEAR_HISTORY, extra_data);
                    if (extra_data.equals("ffff000c5452414e53504f52543d320daaaa")) {
                        /**
                         * 发送回复出厂设置
                         */
                        if (mUARTInterface != null) {
                            final byte[] buffer = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x79, 0x00, 0x00, 0x00, 0x01, (byte) 0x5d, (byte) 0xc0, (byte) 0xAA, (byte) 0xAA};
                            mUARTInterface.send(buffer);
                        }
                    } else if (extra_data.equals("ffff02080179000000015dc0aaaa")) {
                        if (mLanguages_key.equals("zh")) {
                            UiUtils.toast("清空历史数据成功");
                        } else {
                            UiUtils.toast("clear history data Successful");
                        }
                    }
                }
                break;
            case 1111:
                MobclickAgent.onEvent(this, Umeng.SETTING_RECEIVE, extra_data);
                if (isFristSend == false) {
                    if (extra_data.equals("ffff000c5452414e53504f52543d320daaaa")) {
                        isFristSend = true;
                        SheZhiFragment.getInstance().handleSet();
                    }
                } else {
                    SheZhiFragment.getInstance().setResult(extra_data);
                }
                break;
        }
    }

    private void sendCanShuDianYa() {
        if (mUARTInterface != null) {
            isCanshu = true;
            final byte[] buffer = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x06, (byte) 0xE0, 0x03, (byte) 0x00, (byte) 0xff, (byte) 0x0e, (byte) 0x4a, (byte) 0xAA, (byte) 0xAA};
            mUARTInterface.send(buffer);
        }
    }


    /**
     * 截取28 - oD数据
     */
    public static String GetValue(String str) {
        String jieguo = null;
        try {
            jieguo = str.substring(str.indexOf("28") + 2, str.indexOf("0d"));
        } catch (Exception e) {
        } finally {
        }
        return jieguo;
    }

    public void endDataTransform() {
        if (mUARTInterface != null) {//01 03 0107 0003 B5F6
            final byte[] user = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x00, 0x0C, 0x54, 0x52, 0x41, 0x4E, 0x53, 0x50, 0x4F, 0x52, 0x54, 0x3d, 0x30, 0x0D, (byte) 0xAA, (byte) 0xAA};
            mUARTInterface.send(user);
        }
    }

    /**
     * 储能电源
     */
    public void sendShiShiData() {
        if (isConnect) {
            mTryCount = 0;
            mList.clear();
            isFristSend = false;
            isRequstPermission = false;
            SharedPreferenceTools.saveInt(UiUtils.getContext(), "isUi", 77);
            final byte[] user = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x00, 0x0C, 0x54, 0x52, 0x41, 0x4E, 0x53, 0x50, 0x4F, 0x52, 0x54, 0x3D, 0x31, 0x0D, (byte) 0xAA, (byte) 0xAA};
            if (mUARTInterface != null) {
                mUARTInterface.send(user);
            }
        }
    }

    public void sendChuNengDianYuan(int dataCount) {
        isFristSend = true;
        if (isConnect) {
            if (mUARTInterface != null) {
                if (dataCount == 0) {
                    //输入电压
                    final byte[] buffer_5 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, 0x01, 0x01, 0x00, 0x01, (byte) 0xD4, 0x36, (byte) 0xAA, (byte) 0xAA};
                    mUARTInterface.send(buffer_5);
                } else if (dataCount == 1) {
                    //电量
                    final byte[] buffer_4 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, 0x01, 0x00, 0x00, 0x02, (byte) 0xC5, (byte) 0xF7, (byte) 0xAA, (byte) 0xAA};
                    mUARTInterface.send(buffer_4);
                } else if (dataCount == 2) {
                    //温度
                    final byte[] buffer_8 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, 0x01, 0x02, 0x00, 0x02, 0x64, 0x37, (byte) 0xAA, (byte) 0xAA};
                    mUARTInterface.send(buffer_8);
                }
            }
        }
    }


    /**
     * 点击发送 - 首页 - 光伏控制的报文
     */
    public void sendGuangFuKongZhi(int dataCount) {
        isFristSend = true;
        if (isConnect) {
            if (mUARTInterface != null) {//01 03 0107 0003 B5F6
                if (dataCount == 0) {
                    final byte[] buffer_1 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, 0x01, 0x07, 0x00, 0x03, (byte) 0xB5, (byte) 0xF6, (byte) 0xAA, (byte) 0xAA};
                    mUARTInterface.send(buffer_1);
                }
            }
        }
    }


    private void sendSheBeiData2(int dataCount) {
        isFristSend = true;
        if (isConnect) {
            if (mUARTInterface != null) {
                if (dataCount == 0) {
                    //设备型号
                    final byte[] buffer_2 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, 0x00, 0x0C, 0x00, 0x08, (byte) 0x84, (byte) 0x0F, (byte) 0xAA, (byte) 0xAA};
                    mUARTInterface.send(buffer_2);
                } else if (dataCount == 1) {
                    //设备版本
                    final byte[] buffer_3 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, 0x00, 0x14, 0x00, 0x04, 0x04, (byte) 0x0D, (byte) 0xAA, (byte) 0xAA};
                    mUARTInterface.send(buffer_3);
                } else if (dataCount == 2) {
                    //设备Sn码
                    final byte[] buffer_4 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, 0x00, 0x18, 0x00, 0x02, 0x44, (byte) 0x0C, (byte) 0xAA, (byte) 0xAA};
                    mUARTInterface.send(buffer_4);
                }
            }
        } else {
            if (mLanguages_key.equals("zh")) {
                UiUtils.toast("设备断开连接");
            } else {
                UiUtils.toast("Device disconnect");
            }
        }
    }

    private void sendCanShuData2(int dataCount) {
        isFristSend = true;
        if (mUARTInterface != null) {
            //设置超压电压、充电限制电压、均衡充电电压、提升充电电压、浮充充电电压、提升充电返回电压、
            //过放返回电压、过放电压、均衡充电时间、提升充电时间、均衡充电间隔、温度补偿系数
            //系统电压
            if (dataCount == 0) {
                final byte[] buffer = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, (byte) 0xe0, 0x03, 0x00, 0x01, (byte) 0x43, (byte) 0xcA, (byte) 0xAA, (byte) 0xAA};
                mUARTInterface.send(buffer);
            } else if (dataCount == 1) {
                //蓄电池标称容量
                final byte[] bufferr = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, (byte) 0xe0, 0x02, 0x00, 0x02, (byte) 0x52, (byte) 0x0b, (byte) 0xAA, (byte) 0xAA};
                mUARTInterface.send(bufferr);
            } else if (dataCount == 2) {
                //----------------------------------------------------------------------------
                //超压电压 - 充电限制电压56
                final byte[] buffer_1 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, (byte) 0xE0, 0x05, 0x00, 0x02, (byte) 0xE3, (byte) 0xCA, (byte) 0xAA, (byte) 0xAA};
                mUARTInterface.send(buffer_1);
            } else if (dataCount == 3) {
                //均衡充电电压 - 提升充电电压78
                final byte[] buffer_2 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, (byte) 0xE0, 0x07, 0x00, 0x02, (byte) 0x42, (byte) 0x0A, (byte) 0xAA, (byte) 0xAA};
                mUARTInterface.send(buffer_2);
            } else if (dataCount == 4) {
                //浮充充电电压 - 提升充电返回电压9A
                final byte[] buffer_3 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, (byte) 0xE0, 0x09, 0x00, 0x02, 0x23, (byte) 0xC9, (byte) 0xAA, (byte) 0xAA};
                mUARTInterface.send(buffer_3);
            } else if (dataCount == 5) {
                //过房返回电压 -   欠压警告电压BC
                final byte[] buffer_4 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, (byte) 0xE0, 0x0B, 0x00, 0x02, (byte) 0x82, (byte) 0x09, (byte) 0xAA, (byte) 0xAA};
                mUARTInterface.send(buffer_4);
            } else if (dataCount == 6) {
                //过放电压 -   放电限制电压DE
                final byte[] buffer_5 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, (byte) 0xE0, 0x0D, 0x00, 0x02, 0x62, (byte) 0x08, (byte) 0xAA, (byte) 0xAA};
                mUARTInterface.send(buffer_5);
            } else if (dataCount == 7) {
                //过放延时时间 -   均衡充电时间1011
                final byte[] buffer_6 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, (byte) 0xE0, 0x10, 0x00, 0x02, (byte) 0xF2, (byte) 0x0E, (byte) 0xAA, (byte) 0xAA};
                mUARTInterface.send(buffer_6);
            } else if (dataCount == 8) {
                //提升充电时间 -   均衡充电间隔1213
                final byte[] buffer_7 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, (byte) 0xE0, 0x12, 0x00, 0x02, 0x53, (byte) 0xCE, (byte) 0xAA, (byte) 0xAA};
                mUARTInterface.send(buffer_7);
            } else if (dataCount == 9) {
                //温度系数14
                final byte[] buffer_8 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, (byte) 0xE0, 0x14, 0x00, 0x02, (byte) 0xB3, (byte) 0xCF, (byte) 0xAA, (byte) 0xAA};
                mUARTInterface.send(buffer_8);
            } else if (dataCount == 10) {
                //储蓄电池类型
                final byte[] buffer_9 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, (byte) 0xE0, 0x04, 0x00, 0x02, (byte) 0xB2, (byte) 0x0A, (byte) 0xAA, (byte) 0xAA};
                mUARTInterface.send(buffer_9);
            }
        }
    }

    /**
     * 系统电压
     *
     * @param data1
     * @return
     */
    private byte[] sendSettingDianYa(String data1) {
        String s1 = UiUtils.toSixString(data1);
        String data = CRCUtils.getData(CRCUtils.calcCrc16(CRCUtils.hexStringToBytes("0106E002" + s1)));
        byte[] bytes = CRCUtils.hexString2Bytes("FFFF02080106E002" + s1 + data + "AAAA");
        return bytes;
    }

    private byte[] setXuDianData(String data1, String pdu) {
        String s1 = UiUtils.toSixString(data1);
        String data = CRCUtils.getData(CRCUtils.calcCrc16(CRCUtils.hexStringToBytes("0106" + pdu + s1)));
        byte[] bytes = CRCUtils.hexString2Bytes("FFFF02080106" + pdu + s1 + data + "AAAA");
        return bytes;
    }


    private void sendLishiData2(int dataCount) {
        isFristSend = true;
        if (isConnect) {
            if (mUARTInterface != null) {
                //蓄电池当天最低电压、蓄电池当天最高电压、当天充电最大电流、
                //当天放电最大电流、当天充电最大功率、当天放电最大功率、当天充电安时数、当天放电安时数、
                //当天发电量、当天用电量
                if (dataCount == 0) {
                    final byte[] buffer_11 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, 0x01, 0x0B, 0x00, 0x03, (byte) 0x75, (byte) 0xF5, (byte) 0xAA, (byte) 0xAA};
                    mUARTInterface.send(buffer_11);
                } else if (dataCount == 1) {
                    //读运行天数、过放次数、充满次数
                    final byte[] buffer_12 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, 0x01, 0x15, 0x00, 0x03, (byte) 0x15, (byte) 0xF3, (byte) 0xAA, (byte) 0xAA};
                    mUARTInterface.send(buffer_12);
                } else if (dataCount == 2) {
                    //读蓄电池总充电安时数和蓄电池总放电安时数
                    final byte[] buffer_13 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, 0x01, 0x18, 0x00, 0x04, (byte) 0xC5, (byte) 0xF2, (byte) 0xAA, (byte) 0xAA};
                    mUARTInterface.send(buffer_13);
                } else if (dataCount == 3) {
                    //读控制器累计发电量和累计放电量
                    final byte[] buffer_14 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, 0x01, 0x1C, 0x00, 0x04, (byte) 0x84, 0x33, (byte) 0xAA, (byte) 0xAA};
                    mUARTInterface.send(buffer_14);
                } else if (dataCount == 4) {
                    //当天发电量 - 当天耗电量
                    final byte[] buffer_15 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, 0x01, 0x13, 0x00, 0x02, (byte) 0x34, 0x32, (byte) 0xAA, (byte) 0xAA};
                    mUARTInterface.send(buffer_15);
                } else if (dataCount == 5) {
                    //充电最大功率 - 放电最大功率
                    final byte[] buffer_17 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, 0x01, 0x0F, 0x00, 0x02, (byte) 0xF5, (byte) 0xF4, (byte) 0xAA, (byte) 0xAA};
                    mUARTInterface.send(buffer_17);
                } else if (dataCount == 6) {
                    //当天充电安时数 - 放电安时数
                    final byte[] buffer_16 = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x02, 0x08, 0x01, 0x03, 0x01, 0x11, 0x00, 0x02, (byte) 0x95, (byte) 0xF2, (byte) 0xAA, (byte) 0xAA};
                    mUARTInterface.send(buffer_16);
                }
            }

        }
    }


    /**
     * 发送逆变器状态
     */
    public void sendZhuYeNiBianQiState() {
        isFristSend = true;
        if (isConnect) {
            if (mUARTInterface != null) {
                final byte[] buffer = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x01, 0x03, 0x51, 0x31, 0x0D, (byte) 0xAA, (byte) 0xAA};
                mUARTInterface.send(buffer);
            }
        }
    }

    /**
     * 用户修改数据的发送
     *
     * @param s
     */
    public void sendChangeData(byte[] s) {
        MobclickAgent.onEvent(this, Umeng.SETTING_DATA_TEST, UiUtils.bytesToHexString(s));
        System.out.println("settingData  " + UiUtils.bytesToHexString(s));
        if (s == null) {
            return;
        }
        if (mUARTInterface != null) {
            MobclickAgent.onEvent(this, Umeng.SETTING_DATA, UiUtils.bytesToHexString(s));
            mUARTInterface.send(s);
            SystemClock.sleep(900);
        }
    }

    /**
     * 用户修改数据的发送
     *
     * @param s
     */
    public void sendChangeData(byte[] s, boolean isFristSend) {
        this.isFristSend = isFristSend;
        MobclickAgent.onEvent(this, Umeng.SETTING_DATA_TEST, UiUtils.bytesToHexString(s));
        System.out.println("settingData  " + UiUtils.bytesToHexString(s));
        if (s == null) {
            return;
        }
        if (mUARTInterface != null) {
            MobclickAgent.onEvent(this, Umeng.SETTING_DATA, UiUtils.bytesToHexString(s));
            mUARTInterface.send(s);
            SystemClock.sleep(900);
        }
    }


    /**
     * 回复出厂设置
     */
    public void sendClearData() {
        isFristSend = true;
        if (isConnect) {
            if (mUARTInterface != null) {
                SharedPreferenceTools.saveInt(UiUtils.getContext(), "isUi", 88);
                final byte[] user = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x00, 0x0C, 0x54, 0x52, 0x41, 0x4E, 0x53, 0x50, 0x4F, 0x52, 0x54, 0x3d, 0x32, 0x0D, (byte) 0xAA, (byte) 0xAA};
                mUARTInterface.send(user);
            }
        }
    }

    public void sendClearHistory() {
        isFristSend = true;
        if (isConnect) {
            if (mUARTInterface != null) {
                SharedPreferenceTools.saveInt(UiUtils.getContext(), "isUi", 99);
                final byte[] user = new byte[]{(byte) 0xFF, (byte) 0xFF, 0x00, 0x0C, 0x54, 0x52, 0x41, 0x4E, 0x53, 0x50, 0x4F, 0x52, 0x54, 0x3d, 0x32, 0x0D, (byte) 0xAA, (byte) 0xAA};
                mUARTInterface.send(user);
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
